//
//  HomeViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/2/15.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "HomeViewController.h"
#import "CustomOneTypeTableViewCell.h"
#import "CustomTwoTypeTableViewCell.h"
#import "CustomThreeTypeTableViewCell.h"
#import "CustomHeadTableViewCell.h"
#import "CustomCommendHeaderTableViewCell.h"
#import "HomeModel.h"
#import "Images_two_averageTableViewCell.h"
#import "Images_three_left_oneTableViewCell.h"
#import "Images_three_lefttwTableViewCell.h"
#import "ProductListsTableViewCell.h"
#import "CommendModel.h"
#import "BannerTitleModel.h"
#import "SearchViewController.h"
@interface HomeViewController ()
@property(nonatomic,strong)HomeModel *homeModel;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"春来庄"];
    [self setNaviRightImgButton:@"nav_search"];
    [self loadHomeData];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}
-(void)clickRightButton:(UIButton*)bt{
    SearchViewController *searchVC=[[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
-(void)loadHomeData{
     [[LKDHud shareHud] showLoadingView];
     __weak typeof(self) weakSelf = self;
   [ApiClient getHomeDataDidFinishLoaded:^(HomeModel *model, NSString *path) {
      [weakSelf.displayView.mj_header endRefreshing];
       [[LKDHud shareHud] hideLoadingView];

       weakSelf.homeModel=model;
       if (![self.view.subviews containsObject:self.displayView]) {
           [self.view addSubview:self.displayView];
       }
       
   } didFailLoaded:^(NSError *error, NSString *path) {
       [[LKDHud shareHud] hideLoadingView];
        [weakSelf.displayView.mj_header endRefreshing];
   }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor whiteColor];
        _displayView.separatorStyle=UITableViewCellSeparatorStyleNone;
        __weak typeof(self) weakSelf = self;
        _displayView.mj_header = [CLZRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf loadHomeData];
        }];

  }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return self.homeModel.modulesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model=self.homeModel.modulesArray[indexPath.row];
    if ([model isKindOfClass:[Image_carouselModel class]]) {
        static NSString *identifier = @"CustomOneTypeTableViewCell";
        CustomOneTypeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomOneTypeTableViewCell"  owner:self options:nil] lastObject];
            
        }
        [cell setCarouselModel:(Image_carouselModel*)model];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;

    }else if ([model isKindOfClass:[Link_iconModel class]]){
        static NSString *identifier = @"CustomTwoTypeTableViewCell";
        CustomTwoTypeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTwoTypeTableViewCell"  owner:self options:nil] lastObject];
            
        }
        
        [cell setIconModel:(Link_iconModel*)model];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;

    }else if ([model isKindOfClass:[Product_listModel class]]){
        
        ProductListsTableViewCell *cell=[ProductListsTableViewCell cellWithTableView:tableView];
         Product_listModel *productsModel=model;
        [cell setProductsModel:productsModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([model isKindOfClass:[Image_bannerModel class]]){
        static NSString *identifier = @"CustomThreeTypeTableViewCell";
        CustomThreeTypeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomThreeTypeTableViewCell"  owner:self options:nil] lastObject];
            
        }
        [cell setBannerModel:model];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;

    }else if ([model isKindOfClass:[Images_two_averageModel class]]){
        Images_two_averageTableViewCell *cell=[Images_two_averageTableViewCell cellWithTableView:tableView];
        Images_two_averageModel *averageModel=model;
        [cell setImages_two_averageModel:averageModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([model isKindOfClass:[Images_three_left_oneModel class]]){
        Images_three_left_oneTableViewCell *cell=[Images_three_left_oneTableViewCell cellWithTableView:tableView];
        Images_three_left_oneModel *averageModel=model;
        [cell setModel:averageModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }else if ([model isKindOfClass:[Images_three_left_twoModel class]]){
        Images_three_lefttwTableViewCell *cell=[Images_three_lefttwTableViewCell cellWithTableView:tableView];
        Images_three_left_twoModel *averageModel=model;
        [cell setModel:averageModel];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }else if ([model isKindOfClass:[CommendModel class]]){
        static NSString *identifier = @"CustomCommendHeaderTableViewCell";
        CustomCommendHeaderTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCommendHeaderTableViewCell"  owner:self options:nil] lastObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }else {
        static NSString *identifier = @"CustomHeadTableViewCell";

        CustomHeadTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                  identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeadTableViewCell"  owner:self options:nil] lastObject];
        }
        BannerTitleModel *titleModel=(BannerTitleModel*)model;
        cell.bannerNameLabel.text=titleModel.title;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id model=self.homeModel.modulesArray[indexPath.row];
    if ([model isKindOfClass:[Image_carouselModel class]]) {
        return 200;
        
    }else if ([model isKindOfClass:[Link_iconModel class]]){
        return 92;
        
    }else if ([model isKindOfClass:[Product_listModel class]]){
        Product_listModel *productsModel=model;
        int count=productsModel.module_data.products.count;
        
        int lineNum=0;
        if (count%2==0) {
            lineNum=count/2;
        }else{
            lineNum=count/2+1;
        }

        return lineNum*240;
        
    }else if ([model isKindOfClass:[Image_bannerModel class]]){
        return 175;
        
    }else if ([model isKindOfClass:[Images_two_averageModel class]]){
        return 416/2;
        
    }else if ([model isKindOfClass:[Images_three_left_oneModel class]]){
        return 216/2;
    }else if ([model isKindOfClass:[Images_three_left_twoModel class]]){
       return 216/2;
    }else if ([model isKindOfClass:[CommendModel class]]){
        return 42;
    }else {
        return 34;
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,0, 0, 0)];
    }
    
}
-(void)viewDidLayoutSubviews
{
    if ([self.displayView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.displayView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.displayView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.displayView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
